package com.nanyang.tms.service.company.impl;

import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.annotation.SystemMessage;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.companypickaddress.*;
import com.nanyang.tms.api.dto.response.companypickaddress.CompanyPickAddressPageResponse;
import com.nanyang.tms.api.dto.response.companypickaddress.CompanyPickAddressResponse;
import com.nanyang.tms.api.service.companypickaddress.CompanyPickAddressRpcService;
import com.nanyang.tms.dao.company.CompanyPickAddressDao;
import com.nanyang.tms.domain.company.CompanyPickAddressDO;
import com.nanyang.tms.domain.company.CompanyPickAddressQuery;
import com.nanyang.tms.domain.company.CompanyPickAddressVO;
import com.nanyang.tms.manager.company.CompanyPickAddressManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;
@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class CompanyPickAddressRpcServiceImpl implements CompanyPickAddressRpcService {


    @Autowired
    private CompanyPickAddressManager companyPickAddressManager;
    @Autowired
    private CompanyPickAddressDao companyPickAddressDao;
    @Override
    public ApiResult<PageBean<CompanyPickAddressPageResponse>> listCompanyPickAddress(CompanyPickAddressQueryRequest param) {
        ApiResult<PageBean<CompanyPickAddressPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyPickAddressQuery query = new CompanyPickAddressQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<CompanyPickAddressVO> companyPickAddressList = companyPickAddressManager.listCompanyPickAddress(query);
            PageBean<CompanyPickAddressPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(companyPickAddressList.getTotalItem());
            BeanUtils.copyListProperties(companyPickAddressList.getData(), resultList.getData(), CompanyPickAddressPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @Override
    public ApiResult<CompanyPickAddressResponse> selectByCode(CompanyPickAddressQueryRequest param) {
        ApiResult<CompanyPickAddressResponse> result = new ApiResult<>();
        try {
            CompanyPickAddressResponse  companyPickAddressResult = new CompanyPickAddressResponse();
            CompanyPickAddressQuery query=new CompanyPickAddressQuery();
            BeanUtils.copyProperties(param, query);
            CompanyPickAddressVO companyPickAddressVo = companyPickAddressDao.selectByCode(query);
            if(Objects.isNull(companyPickAddressVo)){
                throw new ServiceException(CommonEnum.NO_CONTENT.getCode(),CommonEnum.NO_CONTENT.getMessage());
            }
            BeanUtils.copyProperties(companyPickAddressVo,  companyPickAddressResult);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData( companyPickAddressResult);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @Override
    public ApiResult<Boolean> saveCompanyPickAddress(CompanyPickAddressAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyPickAddressDO companyPickAddressDo = new  CompanyPickAddressDO();
            BeanUtils.copyProperties(param,companyPickAddressDo);
            companyPickAddressDo.setGmtCreatePin(param.getPin());
            companyPickAddressManager.saveCompanyPickAddress(companyPickAddressDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<Boolean> updateCompanyPickAddress(CompanyPickAddressUpdateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyPickAddressDO companyPickAddressDo = new  CompanyPickAddressDO();
            BeanUtils.copyProperties(param,companyPickAddressDo);
            companyPickAddressDo.setGmtModifiedPin(param.getPin());
            companyPickAddressDao.update(companyPickAddressDo);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }


    @Override
    public ApiResult<Boolean> updateDefault(CompanyPickAddressDefautRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            CompanyPickAddressDO companyPickAddressDO = new  CompanyPickAddressDO();
            BeanUtils.copyProperties(param,companyPickAddressDO);
            companyPickAddressDO.setGmtModifiedPin(param.getPin());
            companyPickAddressManager.updateDefault(companyPickAddressDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @Override
    public ApiResult<Boolean> delCompanyPickAddress(CompanyPickAddressDelRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            CompanyPickAddressDO companyPickAddressDO=new CompanyPickAddressDO();
            BeanUtils.copyProperties(param,companyPickAddressDO);
            companyPickAddressDO.setGmtModifiedPin(param.getPin());
            companyPickAddressDao.delPickAddress(companyPickAddressDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
}
